struct Time {The keyword struct introduces the structure definition. The identifier Time is the structure tag that names the structure definition and is used to declare variables of the structure type. In this example, the new type nameint hour; // 0-23
int minute; // 0-59
int second; // 0-59
};
Time timeObject, timeArray[ 10 ], *timePtrdeclares timeObject to be a variable of type Time, timeArray to be an array with 10 elements of type Time, timePtr to be a pointer to a Time object and timeRef to be a reference to a Time object that is initialized with timeObject.&timeRef = timeObject;
cout << timeObject.hour;To print member hour of the structure referenced by timeRef use the statement
cout << timeRef.hour;
timePtr = &timeObject;The expression timePtr->hour is equivalent to (*timePtr).hour which dereferences the pointer and accesses the member hour using the dot operator.cout << timePtr->hour;
Time sunset,// object of type Time
arrayOfTimes[ 5 ],
// array of Time objects
*pointerToTime,
// pointer to a Time objectThe class name becomes a new type specifier. There may be many objects of a class, just as there may be many variables of a type such as int. The programmer can create new class types as needed. This is one reason why C++ is said to be an extensible language.&dinnerTime = sunset;
// reference to a Time object
// prevent multiple inclusions of header fileWhen we build larger programs, other definitions and declarations will also be placed in header files. The preceding preprocessor directives prevent the code between #ifndef and #endif from being included if the name TIME1_H has been defined. If the header has not#ifndef TIME1_H
#define TIME1_H
...
#endif
void ~Time( int );b) The following is a partial definition of class Time.
class Time {public:
// function prototypes
private:
int hour = 0;
int minute = 0;
int second = 0;
};c) Assume the following prototype is declared in class Employee.
int Employee( const char *, const char * );
realPart + imaginaryPart * iwhere i is